package com.ad.pet.app.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.login.fragment.AccountLoginContract;
import com.ad.pet.app.login.fragment.AccountLoginPresenter;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.models.remote.User;
import com.ad.pet.ui.InputView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by liuyixiang on 2019/10/3.
 */

public class ForgetPwdActivity extends AppToolbarActivity implements AccountLoginContract.View {

    @BindView(R.id.input_account)
    InputView inputAccount;
    @BindView(R.id.input_phone_code)
    InputView inputPhoneCode;
    @BindView(R.id.input_pwd)
    InputView inputPwd;
    @BindView(R.id.iv_pic_code)
    ImageView ivPicCode;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private boolean isEye;
    boolean isCanCode = true;
    @Inject
    RxDisposable mRxDisposable;
    @Inject
    AccountLoginPresenter mAccountPresenter;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_forget;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mAccountPresenter.onCreate(this);
        setEye();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        ivPicCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isEye = !isEye;
                setEye();
            }
        });

        inputPhoneCode.getTvRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(inputAccount.getContent())) {
                    ToastUtil.getToastUtil().showShort(inputPhoneCode.getEtContent().getHint().toString());
                    return;
                }
                if (isCanCode) {
                    isCanCode = false;
                    mAccountPresenter.getEmailCode(inputAccount.getContent(), "2", "60", "2");
                }
            }
        });

        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(inputAccount.getContent())) {
                    ToastUtil.getToastUtil().showShort(inputAccount.getEtContent().getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(inputPhoneCode.getContent())) {
                    ToastUtil.getToastUtil().showShort(inputPhoneCode.getEtContent().getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(inputPwd.getContent())) {
                    ToastUtil.getToastUtil().showShort(inputPwd.getEtContent().getHint().toString());
                    return;
                }
                if (inputPwd.getContent().length() < 6 || inputPwd.getContent().length() > 16) {
                    ToastUtil.getToastUtil().showShort("密码请输入6-16个字符");
                    return;
                }

                mAccountPresenter.forgetPwdReset(inputAccount.getContent(), inputPhoneCode.getContent(), inputPwd.getContent());

            }
        });
    }

    @Override
    public void initInject() {
        DaggerLoginComponet.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
        ;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {
        isCanCode = true;
        ToastUtil.getToastUtil().showShort(msg);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void phoneLoginSuccess(User user) {

    }

    @Override
    public void accountLoginSuccess(User user) {

    }

    @Override
    public void getEmailCodeSuccess() {
        ToastUtil.getToastUtil().showShort("获取验证码成功");
        mRxDisposable.add(Observable.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
                .take(59)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        isCanCode = false;
                        String format = String.format("%ds", 60 - aLong);
                        RxTextView.text(inputPhoneCode.getTvRight()).call(format);
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        isCanCode = true;
                        RxTextView.text(inputPhoneCode.getTvRight()).call(getString(R.string.get_phone_verify_code));


                    }
                }));
    }

    @Override
    public void forgetPwdResetSuccess() {
        ToastUtil.getToastUtil().showShort("密码重置成功，请重新登录");
        finish();

    }

    @Override
    public void resetPwdSuccesss() {

    }

    @Override
    public void registJpushSuccess() {

    }

    private void setEye() {

        if (isEye) {
            ivPicCode.setImageResource(R.drawable.ic_eye);
            inputPwd.getEtContent().setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            ivPicCode.setImageResource(R.drawable.ic_uneye);
            inputPwd.getEtContent().setTransformationMethod(PasswordTransformationMethod.getInstance());
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mRxDisposable != null)
            mRxDisposable.clear();
    }
}
